package com.xiaojianone.controller;

import com.xiaojianone.mapper.UserMapper;
import com.xiaojianone.pojo.User;
import com.xiaojianone.service.UserService;
import com.xiaojianone.utils.JwtHelper;
import com.xiaojianone.utils.Result;
import com.xiaojianone.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName  UserController
 * Author  XiaojianOne
 * Date  2025/2/18 14:47
 * Version  8.0
 */

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private UserService userService;


    @PostMapping("login")
    public Result login(@RequestBody User user){

        Result result =userService.login(user);

        return result;
    }


    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token){

        Result result = userService.getUserInfo(token);

        return result;
    }


    @PostMapping("checkUserName")
    public Result checkUserName(@RequestParam String username){

        Result result = userService.checkUserName(username);

        return result;
    }


    @PostMapping("regist")
    public Result regist(@RequestBody User user){

        Result result =userService.regist(user);

        return result;
    }


    @GetMapping("checkLogin")
    public Result checkLogin(@RequestHeader String token){

        boolean expiration = jwtHelper.isExpiration(token);

        if (expiration) {
            //token 已经过期
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        return Result.ok(null);
    }
}
